package com.johnson.news.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.johnson.news.BR;
import com.johnson.news.R;
import com.johnson.news.databinding.ActivityMyCollectBinding;
import com.johnson.news.event_msg.CollectStatusChangedMsg;
import com.johnson.news.model.bean.DataNewsStatus;
import com.johnson.news.model.bean.NewsItem;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.BaseRecyclerAdapter;
import com.youmbe.library.util.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Iterator;

public class MyCollectActivity extends BaseBindingActivity<ActivityMyCollectBinding> {

//    String currentLoginName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        currentLoginName = NewsPreference.getinstance(this).getLoginName();
//
//        if(TextUtils.isEmpty(currentLoginName)) {
//            MyToast.showToast("您还没有登录", this);
//            finish();
//            return;
//        }

        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    BaseRecyclerAdapter<NewsItem> adapter;
    @Override
    protected void init() {
        dataBinding.collectList.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.collectList.setLoadingMoreEnabled(false);
        dataBinding.collectList.setPullRefreshEnabled(false);
        adapter = new BaseRecyclerAdapter<>(this, R.layout.adapter_news, com.johnson.news.BR.data);
        dataBinding.collectList.setAdapter(adapter);

        adapter.setBindingVariable(BR.onLongClickListener, new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
//                Object object = v.getTag(R.id.adapter_tag_data);
//                if(object != null && object instanceof NewsItem) {
//                    final NewsItem newsItem = (NewsItem) object;
//                    AlertDialog.Builder builder = new AlertDialog.Builder(activityInstance);
//                    builder.setMessage("确定要删除吗？");
//                    builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            Utils.newsStatusMap.remove(newsItem.getItemid());
//                            Utils.saveNewsStatus(activityInstance);
//                            adapter.removeData(newsItem);
//                        }
//                    });
//                    builder.setNegativeButton(R.string.cancel, null);
//                    builder.show();
//                }
                return false;
            }
        });

        adapter.setBindingViewClickListener(BR.onClickListener, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object object = v.getTag(R.id.adapter_tag_data);
                if(object != null && object instanceof NewsItem) {
                    NewsItem newsItem = (NewsItem) object;
                    Intent intent = new Intent(activityInstance, NewsDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("data", newsItem);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
        getData();
    }

    void getData() {
        ArrayList<NewsItem> dataList = new ArrayList<>();
        if(Utils.newsStatusMap != null && Utils.newsStatusMap.size() > 0) {
            Iterator<String> iterator = Utils.newsStatusMap.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                DataNewsStatus dataNewsStatus = Utils.newsStatusMap.get(key);
                if(dataNewsStatus.collected) {// && key.contains(currentLoginName)
                    NewsItem item = dataNewsStatus.newsItem;
                    if (item != null) {
                        item.id = Utils.parseInt(item.getItemid());
                        dataList.add(item);
                    }
                }
            }

            if(dataList.size() > 0) {
                dataBinding.askNoData.setVisibility(View.GONE);
                dataBinding.collectList.setVisibility(View.VISIBLE);
                adapter.setData(dataList);
            } else {
                dataBinding.askNoData.setVisibility(View.VISIBLE);
                dataBinding.collectList.setVisibility(View.GONE);
            }
        }
    }


    @Subscribe
    public void onCollectStatusChanged(CollectStatusChangedMsg msg) {
        getData();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_collect;
    }
}
